/*
	FileName : app_usb.c
	Description : 
	
	Created on 2013.11.15 by ZZP.
	
	Revision History:
	
*/



/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "app_usb.h"
#include "bsp.h"
///#include "app_sd.h"
#include "app.h"
#include "diskio.h"
#include "ucos_ii.h"
#include "ff.h"       /* FATFS */
///#include "usbh_msc_core.h"
///#include "usbh_msc_scsi.h"
///#include "usbh_msc_bot.h"
#include "MiscFnx.h"

#include "usbh_msc.h"

#include "ff_gen_drv.h"
#include "usbh_diskio.h"


FATFS USBDISKFatFs;           /* File system object for USB disk logical drive */
FIL MyFile;                   /* File object */
char USBDISKPath[4];          /* USB Host logical drive path */
USBH_HandleTypeDef hUSB_Host; /* USB Host handle */

typedef enum {
  APPLICATION_IDLE = 0,  
  APPLICATION_START,    
  APPLICATION_RUNNING,
}MSC_ApplicationTypeDef;

MSC_ApplicationTypeDef Appli_state = APPLICATION_IDLE;

/* Private function prototypes -----------------------------------------------*/ 
static void SystemClock_Config(void);
static void Error_Handler(void);
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
static void MSC_Application(void);


///////////////////////////////////////////////////////////////////////////////////////
///						Global function realizations
///////////////////////////////////////////////////////////////////////////////////////
void AppTaskUsb(void *p_arg)
{
	(void)p_arg;
		
	while (DEF_TRUE){
		
		if (AppInfo.RdyFlag == 1)
			break;
		
		OSTimeDly(10);
	}
		
	DERESET_ULPI;
	OSTimeDly(100);
	
	if(FATFS_LinkDriver(&USBH_Driver, USBDISKPath, DRV_USB) == 0){
		
		/*##-2- Init Host Library ################################################*/
		USBH_Init(&hUSB_Host, USBH_UserProcess, 0);
    
		/*##-3- Add Supported Class ##############################################*/
		USBH_RegisterClass(&hUSB_Host, USBH_MSC_CLASS);
    
		/*##-4- Start Host Process ###############################################*/
		USBH_Start(&hUSB_Host);
    
		/*##-5- Run Application (Blocking mode) ##################################*/
		while (1){ 
			
			/* USB Host Background task */
			USBH_Process(&hUSB_Host);
      
			/* Mass Storage Application State Machine */
			switch(Appli_state){
				
				case APPLICATION_START:
					MSC_Application();
					Appli_state = APPLICATION_IDLE;
					break;
        
				case APPLICATION_IDLE:
				default:
					break;      
			}
			
			OSTimeDly(1);
		}
  }
  
  for (;;){}
	
}


static void MSC_Application(void)
{
  FRESULT res;                                          /* FatFs function common result code */
  uint32_t byteswritten, bytesread;                     /* File write/read counts */
  uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
  uint8_t rtext[100];                                   /* File read buffer */
  
  /* Register the file system object to the FatFs module */
  if(f_mount(DRV_USB, &USBDISKFatFs) != FR_OK)
  {
    /* FatFs Initialization Error */
    Error_Handler();
  }
  else
  {
    /* Create and Open a new text file object with write access */
    if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) 
    {
      /* 'STM32.TXT' file Open for write Error */
      Error_Handler();
    }
    else
    {
      /* Write data to the text file */
      res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
      
      if((byteswritten == 0) || (res != FR_OK))
      {
        /* 'STM32.TXT' file Write or EOF Error */
        Error_Handler();
      }
      else
      {
        /* Close the open text file */
        f_close(&MyFile);
        
        /* Open the text file object with read access */
        if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
        {
          /* 'STM32.TXT' file Open for read Error */
          Error_Handler();
        }
        else
        {
          /* Read data from the text file */
          res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
          
          if((bytesread == 0) || (res != FR_OK))
          {
            /* 'STM32.TXT' file Read or EOF Error */
            Error_Handler();
          }
          else
          {
            /* Close the open text file */
            f_close(&MyFile);
            
            /* Compare read data with the expected data */
            if((bytesread != byteswritten))
            {                
              /* Read data is different from the expected data */
              Error_Handler();
            }
            else
            {
              /* Success of the demo: no error occurrence */
              BSP_LED_On(LED_WORK);
            }
          }
        }
      }
    }
  }
  
  /* Unlink the USB disk I/O driver */
  ///FATFS_UnLinkDriver(USBDISKPath);
}

/**
  * @brief  User Process
  * @param  phost: Host handle
  * @param  id: Host Library user message ID
  * @retval None
  */
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)
{  
  switch(id)
  { 
  case HOST_USER_SELECT_CONFIGURATION:
    break;
    
  case HOST_USER_DISCONNECTION:
    Appli_state = APPLICATION_IDLE;
    BSP_LED_Off(LED_WORK); 
    BSP_LED_Off(LED_COPY);  
    f_mount(DRV_USB, NULL);          
    break;
    
  case HOST_USER_CLASS_ACTIVE:
    Appli_state = APPLICATION_START;
    break;
    
  default:
    break; 
  }
}

void Error_Handler(void)
{
	
}




/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/






























